
package com.example.caorl.job1.conc0302.lock.reentrant;

import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

public class ReentrantLockDemo {

    public static void syncReentrantLockTest() {
        synchronized (ReentrantLockDemo.class) {
            System.out.println("synchronized 第0次获取锁");
            for (int i = 0; i < 10; i++) {
                synchronized (ReentrantLockDemo.class) {
                    System.out.println("synchronized 第" + (i + 1) + "次次获取锁");
                }
            }

        }
    }

    public static void reentrantLockTest() {
        ReentrantLock reentrantLock = new ReentrantLock();
        reentrantLock.lock();
        System.out.println("ReentrantLock 第0次获取锁");
        for (int i = 0; i < 10; i++) {
            reentrantLock.lock();
            System.out.println("ReentrantLock 第" + (i + 1) + "次次获取锁");
        }
    }

    public static void reentrantWriteLockTest() {
        ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock();
        reentrantReadWriteLock.writeLock().lock();
        System.out.println("reentrantReadWriteLock 第0次获取锁");
        for (int i = 0; i < 10; i++) {
            reentrantReadWriteLock.writeLock().lock();
            System.out.println("reentrantReadWriteLock 第" + (i + 1) + "次次获取锁");
        }
    }

    public static void main(String[] args) {
        syncReentrantLockTest();
        reentrantLockTest();
        reentrantWriteLockTest();
        /*final ReentrantLockCount count = new ReentrantLockCount();

        for (int i = 0; i < 2; i++) {
            new Thread() {
                public void run() {
                    count.get();
                }
            }.start();
        }

        for (int i = 0; i < 2; i++) {
            new Thread() {
                public void run() {
                    count.put();
                }
            }.start();
        }*/
    }
}
